package org.scohen.juploadr.app.authentication;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.ui.ReusableUIFactory;

public class AuthorizationDialog extends Dialog {
    String headerText;
    String descriptionText;
    String idiotLight;

    private Label headerLabel;
    private Label descriptionLabel;
    private Label idiotLabel;
    private Button authorize;
    private Button cancel;
    private Composite buttons;
    private boolean authOk = false;
    private Shell shell;
    private String buttonText;

    public AuthorizationDialog(String headerText, String descriptionText,
            String idiotText, String buttonText, Shell parent) {
        super(parent);
        this.headerText = headerText;
        this.descriptionText = descriptionText;
        this.idiotLight = idiotText;
        this.buttonText = buttonText;
    }

    public boolean open() {
        Shell parent = getParent();
        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText(getText());
        // Your code goes here (widget creation, set result, etc).
        GridLayout layout = new GridLayout();
        layout.marginBottom = 5;
        layout.marginTop = 5;
        shell.setLayout(layout);
        headerLabel = new Label(shell, SWT.SHADOW_NONE);
        headerLabel.setFont(ReusableUIFactory.getInstance()
                .getAuthorizeHeaderFont());
        headerLabel.setText(headerText);
        descriptionLabel = new Label(shell, SWT.SHADOW_NONE);
        descriptionLabel.setText(descriptionText);
        buttons = new Composite(shell, SWT.NONE);

        GridLayout buttonLayout = new GridLayout();
        buttonLayout.numColumns = 2;
        buttonLayout.marginLeft = 20;
        buttons.setLayout(buttonLayout);
        GridData data = new GridData();
        data.grabExcessHorizontalSpace = true;
        data.horizontalAlignment = GridData.END;
        data.horizontalIndent = 5;
        buttons.setLayoutData(data);

        authorize = new Button(buttons, SWT.PUSH);
        authorize.setText(buttonText);

        authorize.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			       AuthorizationDialog.this.authOk = true;
	                AuthorizationDialog.this.shell.dispose();
			}
		});
        cancel = new Button(buttons, SWT.PUSH);
        cancel.setText(Messages.getString("juploadr.ui.button.cancel")); //$NON-NLS-1$
        cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
                AuthorizationDialog.this.shell.dispose();
                }
		});
        idiotLabel = new Label(shell, SWT.SHADOW_NONE);
        idiotLabel.setText(idiotLight);
        idiotLabel.setFont(ReusableUIFactory.getInstance()
                .getAuthorizationFooterFont());
        shell.pack();
        shell.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent event) {
                headerLabel.dispose();
                descriptionLabel.dispose();
                authorize.dispose();
                idiotLabel.dispose();
                cancel.dispose();
                buttons.dispose();

            }
        });

        shell.open();
        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        return authOk;
    }
}
